package com.turing.post.organization.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.BankManage;
import com.turing.post.organization.util.BankManageBusiness;
import com.turing.post.organization.util.BankManageDao;
import com.turing.post.thirdinterface.util.ThirdInterfaceApplicationException;
import com.turing.post.thirdinterface.util.ThirdInterfaceRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Apr 9, 2013
 */
@Stateless(mappedName = "organization.impl.BankManageBusinessImpl")
@Remote
@Local
public class BankManageBusinessImpl implements BankManageBusiness {

	@EJB(mappedName = "organization.impl.BankManageDaoImpl")
	private BankManageDao bankManageDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new ThirdInterfaceApplicationException(
				ThirdInterfaceApplicationException.ERRCODE_CATEGORY_APPLICATION,
				ThirdInterfaceRespCode.RC_ARGS_NULL, error);
	}

	@Override
	public void insertBankManagement(PCPSWebInput input, BankManage bmg)
			throws PCPSApplicationException {
		/**
		 * 参数合法性检查
		 */
		if (input.getOperId() == null
				|| PCPSUtil.isEmpty(input.getOperBranchNo())) {
			errException("登录用户为空!");
		}
		if (bmg == null) {
			errException("银行管理信息为空!");
		}

		if (PCPSUtil.isEmpty(bmg.getBankName())) {
			errException("银行名称为空!");
		}

		if (PCPSUtil.isEmpty(bmg.getServiceAddr())) {
			errException("服务地址为空!");
		}

		if (PCPSUtil.isEmpty(bmg.getJNDIName())) {
			errException("JNDI名称为空!");
		}

		bmg.setBankManageId(commonDaoImpl.getUniqueIntegerKey());
		bmg.setIssueCompanyNo(input.getOperBranchNo().substring(0, 6));
		bankManageDaoImpl.insert(bmg);
	}

	@Override
	public void updateBankManagement(PCPSWebInput input, BankManage bmg)
			throws PCPSApplicationException {
		/**
		 * 参数合法性检查
		 */
		if (input.getOperId() == null) {
			errException("登录用户为空!");
		}
		if (bmg == null) {
			errException("银行管理信息为空!");
		}

		if (PCPSUtil.isEmpty(bmg.getBankName())) {
			errException("银行名称为空!");
		}

		if (PCPSUtil.isEmpty(bmg.getServiceAddr())) {
			errException("服务地址为空!");
		}

		BankManage updateBmg = bankManageDaoImpl
				.getBankManageByBankManageId(bmg.getBankManageId());
		if (updateBmg == null) {
			errException("银行管理编号错误，系统中不存在该编号!");
		}

		updateBmg.setJNDIName(bmg.getJNDIName());
		updateBmg.setBankName(bmg.getBankName());
		updateBmg.setServiceAddr(bmg.getServiceAddr());
		updateBmg.setRemark(bmg.getRemark());

		bankManageDaoImpl.update(updateBmg);
	}

}
